package com.briup.dataset.service.impl;

import com.briup.common.exception.CustomerException;
import com.briup.common.web.MessageStatus;
import com.briup.dataset.constant.DataSetConstant;
import com.briup.dataset.entity.Classify;
import com.briup.dataset.entity.Entity;
import com.briup.dataset.mapper.ClassifyMapper;
import com.briup.dataset.mapper.extend.ClassifyExtendMapper;
import com.briup.dataset.mapper.extend.DataSetExtendMapper;
import com.briup.dataset.mapper.extend.EntityExtendMappper;
import com.briup.dataset.service.IClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类服务类实现类
 */
@Service
public class ClassifyServiceImpl implements IClassifyService {
    @Autowired
    private ClassifyMapper classifyMapper;
    @Autowired
    private ClassifyExtendMapper classifyExtendMapper;
    @Autowired
    private EntityExtendMappper entityExtendMappper;
    @Autowired
    private DataSetExtendMapper dataSetExtendMapper;

    @Transactional
    @Override
    public void insertClassify(Classify classify) throws CustomerException {
        Classify classifyFromDB = classifyExtendMapper.selectByClassifyName(classify.getClassifyName());
        if(classifyFromDB != null){
            throw new CustomerException(MessageStatus.DATA_EXISTED);
        }
        // 封装完整的classify对象
        classify.setClassifyTime(new Date());
        try {
            classifyMapper.insert(classify);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
     * @Description 查看分类下的图片 uuid
     * @Param [classifyId] 分类Id
     * @return uuid集合
     **/
    @Override
    public List<Entity> showImagesInClassify(Integer classifyId) {
        return  entityExtendMappper.showImagesInClassify(classifyId);
    }

    /*
     * @Description 删除某数据集下的分类 此处需要级联删除图片
     * @Param [classifyId] 数据集id
     **/
    @Transactional
    @Override
    public void deleteClassifyById(Integer datasetId,List<Integer> classifyIds) {
        for (Integer classifyId:classifyIds) {
            // 分类下的图片实体
            List<Entity> entityList = entityExtendMappper.showImagesInClassify(classifyId);
            if(entityList != null && entityList.size() > 0){
                List<Integer> entityIdList = entityList.stream().map(Entity::getId).collect(Collectors.toList());
                // 批量删除分类下的所有实体
                entityExtendMappper.deleteInBatch(entityIdList);
            }
            // 删除分类
            classifyMapper.deleteByPrimaryKey(classifyId);
            // 最后判断该数据集下有无分类,如果没有分类了,那么此时数据集为初始化状态
            List<Classify> classifyList = classifyExtendMapper.selectByDataSetId(datasetId);
            if(classifyId == null || classifyList.size() == 0){
                dataSetExtendMapper.updateDatasetStatus(datasetId, DataSetConstant.DATASET_STATUS_INIT);
            }
        }
    }


}
